<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="#####动态代理 AOP概念解释AOP用在哪些方面：AOP能够将那些与业务无关，却为业务模块所共同调用的逻辑或责任，例如事务处理、日志管理、权限控制，异常处理等，封装起来，便于减少系统的重复代码，降低模块间的耦合度，并有利于未来的可操作性和可维护性。">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-AOP(面向切面编程)实现">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Java-Special-Function/Java-AOP(面向切面编程)实现/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="#####动态代理 AOP概念解释AOP用在哪些方面：AOP能够将那些与业务无关，却为业务模块所共同调用的逻辑或责任，例如事务处理、日志管理、权限控制，异常处理等，封装起来，便于减少系统的重复代码，降低模块间的耦合度，并有利于未来的可操作性和可维护性。">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-476ce36a7d0fee24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-54db06518d9bb85d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-f68e97d75999462f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:38:29.318Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java-AOP(面向切面编程)实现">
<meta name="twitter:description" content="#####动态代理 AOP概念解释AOP用在哪些方面：AOP能够将那些与业务无关，却为业务模块所共同调用的逻辑或责任，例如事务处理、日志管理、权限控制，异常处理等，封装起来，便于减少系统的重复代码，降低模块间的耦合度，并有利于未来的可操作性和可维护性。">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-476ce36a7d0fee24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Java-Special-Function/Java-AOP(面向切面编程)实现/"/>





  <title>Java-AOP(面向切面编程)实现 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Java-Special-Function/Java-AOP(面向切面编程)实现/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java-AOP(面向切面编程)实现</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>#####<a href="https://www.jianshu.com/p/2f33447f8b58" target="_blank" rel="noopener">动态代理</a></p>
<h2 id="AOP概念解释"><a href="#AOP概念解释" class="headerlink" title="AOP概念解释"></a>AOP概念解释</h2><p>AOP用在哪些方面：AOP能够将那些与业务无关，却为业务模块所共同调用的逻辑或责任，例如<strong>事务处理、日志管理、权限控制，异常处理</strong>等，封装起来，便于减少系统的重复代码，降低模块间的耦合度，并有利于未来的可操作性和可维护性。<br><a id="more"></a></p>
<h2 id="AOP中的概念"><a href="#AOP中的概念" class="headerlink" title="AOP中的概念"></a>AOP中的概念</h2><p><strong>Aspect(切面)</strong>：指横切性关注点的抽象即为切面，它与类相似，只是两者的关注点不一样，类是对物体特征的抽象，而切面是横切性关注点的抽象。<br><strong>joinpoint(连接点)</strong>：所谓连接点是指那些被拦截到的点（<strong>可以是方法、属性、或者类的初始化时机(可以是Action层、Service层、dao层)</strong>）。在Spring中，这些点指的是方法，因为Spring只支持方法类型的连接点，实际上joinpoint还可以是field或类构造器。<br><strong>Pointcut(切入点)</strong>：所谓切入点是指我们要对那些joinpoint进行拦截的定义，也即joinpoint的集合。<br><strong>Advice(通知)</strong>：所谓通知是指拦截到joinpoint之后所要做的事情就是通知。通知分为前置通知、后置通知、异常通知、最终通知、环绕通知。我们就以CGlibProxyFactory类的代码为例进行说明：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">public class CGlibProxyFactory implements MethodInterceptor &#123;</span><br><span class="line">    private Object targetObject; // 代理的目标对象</span><br><span class="line"></span><br><span class="line">    public Object createProxyInstance(Object targetObject) &#123;</span><br><span class="line">        this.targetObject = targetObject; </span><br><span class="line"></span><br><span class="line">        Enhancer enhancer = new Enhancer();</span><br><span class="line">        enhancer.setSuperclass(this.targetObject.getClass()); // 设置目标类为代理对象的父类</span><br><span class="line">        enhancer.setCallback(this);</span><br><span class="line"></span><br><span class="line">        return enhancer.create();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    // 从另一种角度看： 整个方法可看作环绕通知</span><br><span class="line">    @Override</span><br><span class="line">    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable &#123;</span><br><span class="line">        PersonServiceBean bean = (PersonServiceBean)this.targetObject;</span><br><span class="line">        Object result = null; </span><br><span class="line">        if (bean.getUser() != null) &#123; // 有权限</span><br><span class="line">            // ...... advice() ----&gt; 前置通知(所谓通知，就是我们拦截到业务方法之后所要干的事情)</span><br><span class="line">            try &#123;</span><br><span class="line">                result = methodProxy.invoke(targetObject, args); // 把方法调用委派给目标对象</span><br><span class="line">                // ...... afteradvice() ----&gt; 后置通知</span><br><span class="line">            &#125; catch (RuntimeException e) &#123;</span><br><span class="line">                // ...... exceptionadvice() ----&gt; 异常通知</span><br><span class="line">            &#125; finally &#123;</span><br><span class="line">                // ...... finallyadvice() ----&gt; 最终通知</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>Target(目标对象)</strong>：代理的目标对象。<br><strong>Weave(织入)</strong>：指将aspects应用到target对象并导致proxy对象创建的过程称为织入。<br><strong>Introduction(引入)</strong>：在不修改类代码的前提下，Introduction可以在运行期为类(代理类)动态地添加一些方法或Field。<br>AOP带来的好处：降低模块的耦合度；使系统容易扩展；更好的代码复用性。</p>
<p>####2. JDK实现<br>1). 创建Person接口<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public interface PersonService &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 保存</span><br><span class="line">	 * @param name 名称</span><br><span class="line">	 */</span><br><span class="line">	public void save(String name);</span><br><span class="line">	/**</span><br><span class="line">	 * 根据ID更新名称</span><br><span class="line">	 * @param name 姓名</span><br><span class="line">	 * @param personId 人员ID</span><br><span class="line">	 */</span><br><span class="line">	public void update(String name, Integer personId);</span><br><span class="line">	/**</span><br><span class="line">	 * 根据ID获取名称</span><br><span class="line">	 * @param personId 人员ID</span><br><span class="line">	 * @return 名称</span><br><span class="line">	 */</span><br><span class="line">	public String getPersonName(Integer personId);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>2). 创建实现Person接口的实现类PersonImpl<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">public class PersonServiceImpl implements PersonService&#123;</span><br><span class="line">	private String user = null;</span><br><span class="line">	</span><br><span class="line">	public void setUser(String user) &#123;</span><br><span class="line">		this.user = user;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public String getUser() &#123;</span><br><span class="line">		return user;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public PersonServiceImpl() &#123;	&#125;</span><br><span class="line">	</span><br><span class="line">	public PersonServiceImpl(String user)&#123;</span><br><span class="line">		this.user = user;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public void save(String name) &#123;</span><br><span class="line">		System.out.println(&quot;我是save方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void update(String name, Integer personId) &#123;</span><br><span class="line">		System.out.println(&quot;我是update方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String getPersonName(Integer personId) &#123;</span><br><span class="line">		System.out.println(&quot;我是getPersonName方法&quot;);</span><br><span class="line">		return &quot;xxx&quot;;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>3). 创建代理类PersonServiceImplProxy<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class PersonServiceImplProxy implements InvocationHandler &#123;</span><br><span class="line">	private PersonService personService;</span><br><span class="line">	</span><br><span class="line">	public PersonService createProxy(PersonService personService) &#123;</span><br><span class="line">		return (PersonService) Proxy.newProxyInstance(PersonServiceImplProxy.class.getClassLoader(), personService.getClass().getClass().getInterfaces(), this);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable &#123;</span><br><span class="line">		PersonServiceImpl pImpl = (PersonServiceImpl) this.personService;</span><br><span class="line">		Object result = null;</span><br><span class="line">		// 如果不等于null则表示有权限</span><br><span class="line">		if (null != pImpl.getUser()) &#123;</span><br><span class="line">			// 执行方法</span><br><span class="line">			result = method.invoke(personService, args);</span><br><span class="line">		&#125;</span><br><span class="line">		return result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>4). 创建Demo类测试<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public class Demo &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		PersonServiceImplProxy proxy = new PersonServiceImplProxy();</span><br><span class="line">		PersonService service = proxy.createProxy(new PersonServiceImpl(&quot;mazaiting&quot;));</span><br><span class="line">		service.save(&quot;123&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-476ce36a7d0fee24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></p>
<p>5). 更改测试代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public class Demo &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		PersonServiceImplProxy proxy = new PersonServiceImplProxy();</span><br><span class="line">		PersonService service = proxy.createProxy(new PersonServiceImpl());</span><br><span class="line">		service.save(&quot;123&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-54db06518d9bb85d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p>####3. <a href="https://sourceforge.net/projects/cglib/files/" target="_blank" rel="noopener">CGlib</a>实现AOP功能</p>
<p>#####动态代理技术只能是基于接口，那如果这个对象没有接口，就要使用CGlib这个工具包。<br>1). 创建PersonService类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">public class PersonService &#123;</span><br><span class="line">	private String user = null;</span><br><span class="line">	</span><br><span class="line">	public void setUser(String user) &#123;</span><br><span class="line">		this.user = user;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public String getUser() &#123;</span><br><span class="line">		return user;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public PersonService() &#123;	&#125;</span><br><span class="line">	</span><br><span class="line">	public PersonService(String user)&#123;</span><br><span class="line">		this.user = user;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public void save(String name) &#123;</span><br><span class="line">		System.out.println(&quot;我是save方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void update(String name, Integer personId) &#123;</span><br><span class="line">		System.out.println(&quot;我是update方法&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getPersonName(Integer personId) &#123;</span><br><span class="line">		System.out.println(&quot;我是getPersonName方法&quot;);</span><br><span class="line">		return &quot;xxx&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>2). 创建CGlibProxyFactory类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">public class CGlibProxyFactory implements MethodInterceptor &#123;</span><br><span class="line">	// 代理的目标对象</span><br><span class="line">	private Object targetObject;</span><br><span class="line">	</span><br><span class="line">	public Object createProxyInstance(Object targetObject) &#123;</span><br><span class="line">		this.targetObject = targetObject;</span><br><span class="line">		</span><br><span class="line">		// 该类用于生成代理对象</span><br><span class="line">		Enhancer enhancer = new Enhancer();</span><br><span class="line">		// 设置目标类为代理对象的父类</span><br><span class="line">		enhancer.setSuperclass(this.targetObject.getClass());</span><br><span class="line">		// 设置回调用对象本身</span><br><span class="line">		enhancer.setCallback(this);</span><br><span class="line">		return enhancer.create();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable &#123;</span><br><span class="line">		PersonService service = (PersonService) this.targetObject;</span><br><span class="line">		Object result = null;</span><br><span class="line">		// 有权限</span><br><span class="line">		if (null != service.getUser()) &#123;</span><br><span class="line">			// 把方法调用委派给目标对象</span><br><span class="line">			result = methodProxy.invoke(targetObject, args);</span><br><span class="line">		&#125;</span><br><span class="line">		return result;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>3). 创建Demo测试类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public class Demo &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		CGlibProxyFactory factory = new CGlibProxyFactory();</span><br><span class="line">		PersonService service = (PersonService) factory.createProxyInstance(new PersonService(&quot;mazaiting&quot;));</span><br><span class="line">		service.save(&quot;999&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>4). 打印结果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-f68e97d75999462f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Java-Framework/Java-JimToMov(图片转视频)/" rel="next" title="Java-JimToMov(图片转视频)">
                <i class="fa fa-chevron-left"></i> Java-JimToMov(图片转视频)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Java-Special-Function/Java-Pinyin4j(汉字转拼音)/" rel="prev" title="Java-Pinyin4j(汉字转拼音)">
                Java-Pinyin4j(汉字转拼音) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#AOP概念解释"><span class="nav-number">1.</span> <span class="nav-text">AOP概念解释</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#AOP中的概念"><span class="nav-number">2.</span> <span class="nav-text">AOP中的概念</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
